package main

import (
	"encoding/json"
	"fmt"
	"github.com/go-resty/resty/v2"
	"io/ioutil"
	"log"
	"strconv"
	"time"
	"yundian/internel/app/helper"
	"yundian/internel/pkg/dbs"
	"yundian/internel/pkg/dingo"
)

func main()  {
	dingo.InitViper("./configs/admin.yaml")
	dbs.NewMysqlComponent().Step()

	url := "http://mng.yundian.ink/mng/10041/user/agent/settlement/orderItem/list.data"
	path := fmt.Sprintf("./data/dividends/%s",time.Now().Format("20060102150405"))
	// 创建
	err := helper.MakeDir(path)
	if err != nil {
		log.Println("创建失败")
	}


	for i := 100000; i <= 101192; i++ {
		doRequestDividends(3000, i, url, path)
	}
}

func doRequestDividends(pageSize, id int, url, path string)  {
	userId := strconv.Itoa(id)
	client := resty.New()
	// 先取一页，看下总数是多少条
	m := map[string]string{}
	m["page"] = strconv.Itoa(1)
	m["limit"] = strconv.Itoa(10)
	m["userId"] = userId  // 用户ID
	resp, err := client.R().
		SetHeader("Content-Type", "application/x-www-form-urlencoded").
		SetFormData(m).
		SetHeader("Cookie", "JSESSIONID=node01rsdbgx8i1bhhx53c73usu9og5.node0").
		//SetResult(placeType).    // or SetResult(AuthSuccess{}).
		Post(url)
	if err != nil {
		log.Println("抓取失败")
	}
	b := resp.Body()
	sett := &settLogGenerated{}
	_ = json.Unmarshal(b, sett)

	if sett.Body.Total > 0 {
		pages := (sett.Body.Total - 1)/pageSize + 1
		if len(sett.Body.Data) > 0 {
			for i := 1; i <= pages; i++ {
				m := map[string]string{}
				m["page"] = strconv.Itoa(i)
				m["limit"] = strconv.Itoa(pageSize)
				m["userId"] = userId // 用户ID
				resp, err := client.R().
					SetHeader("Content-Type", "application/x-www-form-urlencoded").
					SetFormData(m).
					SetHeader("Cookie", "JSESSIONID=node01rsdbgx8i1bhhx53c73usu9og5.node0").
					//SetResult(placeType).    // or SetResult(AuthSuccess{}).
					Post(url)
				if err != nil {
					log.Println("抓取失败")
				}
				err = ioutil.WriteFile(fmt.Sprintf(path + "/%s-%d.json", userId, i), resp.Body(),666)
				if err != nil {
					log.Println(err.Error())
				}
			}
		}
	}
}



type settLogGenerated struct {
	Code int `json:"code"`
	Body struct {
		Total int `json:"total"`
		Data  []struct {
			ID                 int    `json:"id"`
			TradeNo            string `json:"tradeNo"`
			UserID             int    `json:"userId"`
			UserName           string `json:"userName"`
			PlaceID            int    `json:"placeId"`
			PlaceName          string `json:"placeName"`
			DeviceSn           string `json:"deviceSn"`
			SharedMode         string `json:"sharedMode"`
			FeeMode            int    `json:"feeMode"`
			DeviceTypeID       int    `json:"deviceTypeId"`
			DeviceTypeName     string `json:"deviceTypeName"`
			RoleType           int    `json:"roleType"`
			ProfitType         int    `json:"profitType"`
			ProfitProportion   int    `json:"profitProportion"`
			Money              int    `json:"money"`
			SettlementMoney    int    `json:"settlementMoney"`
			State              int    `json:"state"`
			UpdateDate         string `json:"updateDate"`
			CreateDate         string `json:"createDate"`
			MoneyStr           string `json:"moneyStr"`
			ProfitTypeName     string `json:"profitTypeName"`
			SettlementMoneyStr string `json:"settlementMoneyStr"`
			WalletType         int    `json:"walletType"`
			RoleTypeName       string `json:"roleTypeName"`
		} `json:"data"`
	} `json:"body"`
}